﻿using ConfExpert.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevTeam;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using System.Globalization;
using ConfExpert.Utility;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class TelaSatinAlma : XForm
    {
        public TelaSatinAlma()
        {
            InitializeComponent();
        }

        private void TelaSatinAlma_Load(object sender, EventArgs e)
        {
            this.Text = "Tela Satınalma";
            gridView1.Init("GetPOTela4");
            gridView1.AddDetailGrid(gridView2, "POTelaId");

            gridView1.AddAutoIncrementalPK("POTelaId");
            gridView2.AddAutoIncrementalPK("POTelaLinesId");

            gridView1.MakeColumnInvisible("POTelaId");
            gridView2.MakeColumnInvisible("POTelaLinesId");
            gridView2.ChangeExpression("LineTotal", "TotalQty*AnlasilanFiyat");

            gridView1.EnableNewRow();

            if(gridView1.RowCount > 0)
                gridView2.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;

            gridView2.GetManager().GridMenu.AddMenu("Sipariş Oluştur", SiparisOlustur);

           // XForm x = new XForm();
            DataTable dtPODurumTela = AppSession.Lookups.PODurum.Select("PODurumId not in(2,3)").CopyToDataTable();
            RepositoryItemGridLookUpEdit riPODurumTela = GetCustomGLE(dtPODurumTela, "Name", "PODurumId");
            gridView2.Columns["PODurumId"].ColumnEdit = riPODurumTela;

            gridView1.DataTable().TableNewRow+= TelaSatinAlma1_TableNewRow;
            gridView2.DataTable().TableNewRow += TelaSatinAlma2_TableNewRow;

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
        }

        void TelaSatinAlma1_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["TalepTarihi"] = DateTime.Now;
            e.Row["UserId"] = WhoAmI;
        }

        void TelaSatinAlma2_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["stGenelId"] = 1;
            e.Row["PODurumId"] = 1;
            e.Row["UserId"] = WhoAmI;
        }

        void SiparisOlustur(object sender, EventArgs e)
        {
            List<int> TelaVendorIdList = new List<int>();
            List<int> POTelaLinesIdList = new List<int>();
            int[] SelectedRowHandles = gridView2.GetSelectedRows();

            for (int i = 0; i < SelectedRowHandles.Length; ++i)
            {
                DataRow dr = gridView2.GetDataRow(SelectedRowHandles[i]);
                int TelaVendorId = dr["TelaVendorId"].ToInt();
                int POTelaLinesId = dr["POTelaLinesId"].ToInt();
                int POSipTelaId = dr["POSipTelaId"].ToInt();

                if (POSipTelaId == 0)
                {
                    if (!TelaVendorIdList.Contains(TelaVendorId))
                        TelaVendorIdList.Add(TelaVendorId);

                    if (!POTelaLinesIdList.Contains(POTelaLinesId))
                        POTelaLinesIdList.Add(POTelaLinesId);
                }
            }

            String paramStrVendorList = ListToCommaSeperatedString(TelaVendorIdList);
            String paramStrPOTelaLinesIds = ListToCommaSeperatedString(POTelaLinesIdList);
            
            ExecuteNonQuery("InsPOSipTela2", gridView1.GetSelectedValues("POTelaId"),  paramStrPOTelaLinesIds, paramStrVendorList, WhoAmI);
            int RowHandle = gridView1.FocusedRowHandle;
            gridView1.RefreshDataSource("GetPOTela4");
            gridView1.FocusedRowHandle = RowHandle;

        }

        private String ListToCommaSeperatedString(List<int> list)
        {
            String retStr = String.Empty;
            /*int[] listeArray = list.ToArray();

            for(int i = 0; i < listeArray.Length; ++i)
            {
                if (retStr.Equals(String.Empty))
                    retStr = listeArray[i].ToStr();
                else
                    retStr += "," + listeArray[i];
            }*/

            foreach (int element in list)
            {
                if (retStr.Equals(String.Empty))
                    retStr = element.ToStr();
                else
                    retStr += "," + element.ToStr();
            }

            return retStr;
        }

        private void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.UpdateAll("", "POTela", "", "POTelaLines");
            gridView1.BestFitColumns();
        }
    }
}
